package com.htj.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.unit.DataUnit;
import com.htj.admin.domain.Food;
import com.htj.admin.domain.Order;
import com.htj.admin.domain.dto.LineDto;
import com.htj.admin.service.FoodServiceInf;
import com.htj.admin.service.OrderServiceInf;
import com.htj.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

@RestController
@RequestMapping("eChart")
public class eChartsController {
    @Autowired
    OrderServiceInf orderServiceInf;
    @Autowired
    FoodServiceInf foodServiceInf;

    @GetMapping("/getLine")
    public Result<Map<String,Object>> getLine(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("一月", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y",CollUtil.newArrayList(250, 230, 224, 218, 135, 147, 260));
        return Result.succ(map);
    }

    @GetMapping("/getLineBySales")
    public Result<List<Integer>> getLineBySales() throws ParseException {
        List<Order> list = orderServiceInf.getAll();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        int q5 = 0;
        int q6 = 0;
        int q7 = 0;
        for(Order order : list){
            Date date = format.parse(order.getCreateTime());
            int week = DateUtil.dayOfWeek(date);
            switch (week){
                case 0 :q1++;break;
                case 1 :q2++;break;
                case 2 :q3++;break;
                case 3 :q4++;break;
                case 4 :q5++;break;
                case 5 :q6++;break;
                case 6 :q7++;break;
                default:break;
            }
        }
        return Result.succ(CollUtil.newArrayList(q1,q2,q3,q4,q5,q6,q7));
    }

    @GetMapping("/getPie")
    public Result<List<Food>> getPie(){
        List<Food> list = foodServiceInf.getPie();
        return Result.succ(list);
    }

}
